package com.itheima.behaviour.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.FollowBehaviourDto;
import com.itheima.behaviour.mapper.ApBehaviorEntryMapper;
import com.itheima.behaviour.mapper.ApFollowBehaviorMapper;
import com.itheima.behaviour.pojo.ApBehaviorEntry;
import com.itheima.behaviour.pojo.ApFollowBehavior;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.constants.SystemConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description <p>APP关注行为 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.behaviour.service.impl
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Resource
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    /**
     * 添加关注行为
     *
     * @param dto
     */
    @Override
    public void saveFollowBehavior(FollowBehaviourDto dto) {
        //1. 查询实体id
        ApBehaviorEntry entry = apBehaviorEntryMapper.findByEntryIdAndType(dto.getUserId(), SystemConstants.TYPE_USER);
        //2. 查询是否存在关注记录
        ApFollowBehavior followBehavior = query().eq("entry_id", entry.getId()).eq("follow_id", dto.getFollowId()).one();
        //3. 如果不存在则添关注记录
        if(null == followBehavior){
            followBehavior = new ApFollowBehavior();
            followBehavior.setFollowId(dto.getFollowId());
            followBehavior.setEntryId(entry.getId());
            followBehavior.setArticleId(dto.getArticleId());
            followBehavior.setCreatedTime(LocalDateTime.now());
            save(followBehavior);
        }
    }
}
